<?php
Yii::import('widgets.portal.HomePortlet');

class CompanyCertificateNotification extends HomePortlet {
	public $title = '公司资质提醒';
	protected $role = 'employeeView';

	public $dayOffsetMin = -2;
	public $dayOffsetMax = 90;
	protected function renderContent() {
		$this->renderNotification('verifyDate');
		$this->renderNotification('extendDate');
		$this->renderNotification('expireDate');
	}

	protected function renderNotification($column) {
		$criteria = new CDbCriteria();
		$criteria->select = '*, ' . "DATEDIFF($column, date(now())) as offset";
		$criteria->order = 'offset ASC';
		$criteria->having = 'offset between :dayOffsetMin and :dayOffsetMax';
		$criteria->params['dayOffsetMin'] = $this->dayOffsetMin;
		$criteria->params['dayOffsetMax'] = $this->dayOffsetMax;

		$model = CompanyCertificate::model();
		$commandBuilder = Yii::app()->db->getSchema()->getCommandBuilder();
		$command = $commandBuilder->createFindCommand($model->getTableSchema(), $criteria);
		$rows = $command->queryAll();

		$this
			->render('companyCertificateNotification',
				array(
					'certificates' => $rows,
					'column' => $column,
					'label' => $model->getAttributeLabel($column)
				));
	}
}
